﻿#include "TcpClientManager.h"
#include <QHostAddress>
#include <QImage>
#include <QThread>

TcpClientManager::TcpClientManager()
{
    std::cout << "TcpClientManager" << std::endl;
    connect(&mTcpSocket, &QTcpSocket::connected, this, &TcpClientManager::connectSlot);
    connect(&mTcpSocket, &QTcpSocket::readyRead, this, &TcpClientManager::readyReadSlot);
    connect(&mTcpSocket, &QTcpSocket::disconnected, this, &TcpClientManager::disconnectSlot);
    set_transferProxy(new TransferProxy(&mTcpSocket));
    //接收者的名字永远叫服务器
    transferProxy->setReceiveName("Server");
}

void TcpClientManager::loginAccount(QString account, QString password)
{
    set_loginStatus("login....");
    connectServer();
    if(mIsConnectFlag) {
        QString sendMessage = QString("login@:account:%1;password:%2\n").arg(account).arg(password);
        mTcpSocket.write(sendMessage.toStdString().c_str());
        set_account(account);
        //客户端发送者就是登录账号
        transferProxy->setSendName(account);
    }
}

void TcpClientManager::registerAccount(QString account, QString password) {
    connectServer();
    if(mIsConnectFlag) {
        QString sendMessage = QString("register@:account:%1;password:%2\n").arg(account).arg(password);
        mTcpSocket.write(sendMessage.toStdString().c_str());
    }
}

void TcpClientManager::disconnectSlot()
{
    mIsConnectFlag = false;
    set_replyCode(-10086);
    set_displayIndex(0);
}

void TcpClientManager::connectSlot()
{

}

void TcpClientManager::readyReadSlot() {
    TransferBase* ptr = transferProxy->isHaveReceiving();
    qDebug() << "TcpClientManager" << ptr;
    //如果还有任务在运行中，比如读图片和电影那么就继续等待 直到完成
    if(ptr) {
        transferProxy->recvInfo("");
    }else {
        QString replyStr = mTcpSocket.readLine();
        qDebug() << "readStr = " << replyStr;
        if(replyStr.contains("login@:")) {
            handleLoginReply(replyStr.remove("login@:"));
        }else if(replyStr.contains("register@:")) {
            handleRegisterReply(replyStr.remove("register@:"));
        }else {
            transferProxy->recvInfo(replyStr);
        }
    }
}

void TcpClientManager::disconectClient()
{
    //断开和服务器的TCP通信， 相当于开始四次挥手了
    mTcpSocket.disconnectFromHost();
}

void TcpClientManager::connectServer()
{
    if(mIsConnectFlag) {
        return;
    }
    mTcpSocket.connectToHost(QHostAddress("127.0.0.1"), 9528);
    if(mTcpSocket.waitForConnected(10000)) {
        std::cout << "connected !" << std::endl;
        mIsConnectFlag = true;
    } else {
        std::cout << "connect out time limit !" << std::endl;
        mIsConnectFlag = false;
    }
}

void TcpClientManager::handleLoginReply(QString replay) {
    QStringList replayList = replay.split(";");
    TestLog(QString("info:%1").arg(replay));
    int code = 0;
    QString info = "";
    bool res = getInfoAndCode(replay, code, info);
    if(res) {
        set_loginStatus(info);
        set_replyCode(code);
    }else {
        set_loginStatus("");
    }
}

void TcpClientManager::handleRegisterReply(QString replay) {
    QStringList replayList = replay.split(";");
    TestLog(QString("info:%1").arg(replay));
    int code = 0;
    QString info = "";
    bool res = getInfoAndCode(replay, code, info);
    if(res) {
        set_replyCode(code);
    }
}

bool TcpClientManager::getInfoAndCode(QString replay, int &code, QString &info)
{
    QStringList replayList = replay.split(";");
    TestLog(QString("info:%1").arg(replay));

    if(replayList.size() != 2) {
        return false;
    }
    QStringList codeList = replayList.at(0).split(":");
    if(codeList.size() == 2) {
        code = codeList.at(1).toInt();
    }
    QStringList infoList =  replayList.at(1).split(":");
    if(infoList.size() == 2) {
        info = infoList.at(1);
    }

    if(codeList.size() != 2 || infoList.size()!= 2) {
        return false;
    }
    return true;
}

